package com.ihighest.app.infox.activity;

import org.json.JSONArray;
import org.json.JSONException;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.RankingTodayAdapter;
import com.ihighest.app.infox.adapter.SelecMyColumnListAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;

/**
 * 
 * RankingTodayActivity
 * 
 * <pre>
 * Description: 今日排行界面
 * </pre>
 * 
 * <pre>
 * Example:
 * </pre>
 * 
 * @author yyh Created at 2015-10-14 下午2:35:02
 * @version 1.0
 */

public class RankingTodayActivity extends BaseActivity implements OnCheckedChangeListener {
	private ActivityTitle title;
	// 专家，企业
	private RadioButton expertRanking, companyRanking;
	private int page = 1;// 数据请求页数，默认为第一页
	private String state = "1";
	private long mTime;
	private PullToRefreshListView rankingListview;
	private JSONArray array;
	private JSONArray moreArray;
	private TextView nodate;
	private RankingTodayAdapter adapter;
	private TextView emptyview;
	//private SelecMyColumnListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_rankingtoday);

		inint();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void inint() {
		title = (ActivityTitle) findViewById(R.id.rankingTodayActivityTitle);
		title.setTitle("今日排行");
		title.setViewGone(title.rightLayout, title.titleRefresh, title.titleMsg);
		title.setViewVersible(title.titleBack);

		nodate = (TextView) findViewById(R.id.noRankingData);
		rankingListview = (PullToRefreshListView) findViewById(R.id.id_ranking_listview);
		 emptyview = new TextView(this);
		emptyview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		emptyview.setText("加载中...");
		emptyview.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
		emptyview.setVisibility(View.GONE);
		rankingListview.setEmptyView(emptyview);
		expertRanking = (RadioButton) findViewById(R.id.id_zj);
		companyRanking = (RadioButton) findViewById(R.id.id_qy);
		expertRanking.setOnCheckedChangeListener(this);
		companyRanking.setOnCheckedChangeListener(this);
		initData();
		companyRanking.setChecked(true);
		rankingListview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {

			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				page++;
				initData();

			}

		});

	}

	/**
	 * 初始化数据
	 * 
	 * 
	 * @author lkx Created at 2015-10-29 下午3:01:27
	 */
	private void initData() {
		ApiParam param = ApiParam.create();
		param.addParam("page", page);
		if (page >= 2) {
			param.addParam("time", mTime);
		}

		param.addParam("type", state);
		Api.get("rankinglist", param, new ApiCallback(RankingTodayActivity.this,true) {

			@Override
			public void handleSuccess(ApiResult result) {

				if (result.getDatas() == null || result.getDatas().length() == 0) {
					// 没有数据时
					if (page == 1) {
						// 当时第一页时，显示无该类型数据
						nodate.setVisibility(View.VISIBLE);
						rankingListview.setVisibility(View.GONE);
					} else {
						// 不是第一页表示数据加载完毕
						page = page > 1 ? --page : 1;
						ToastKit.showShort(RankingTodayActivity.this, "数据加载完毕");
					}

					emptyview.setText("暂无数据");
				} else {
					nodate.setVisibility(View.GONE);
					rankingListview.setVisibility(View.VISIBLE);
					if (page == 1) {
						array = result.getDatas();
						adapter.setData(result.getDatas());
					} else {

						moreArray = result.getDatas();
						try {
							array = AppKit.joinJSONArray(array, moreArray);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						adapter.appendData(moreArray);
					}
					mTime = result.getTime();

				}
				if(result.getCode()!=100){
					emptyview.setText("加载失败，稍后重试");
				}

				rankingListview.onRefreshComplete();
			}

			@Override
			public void handleFailure(Exception e, String msg) {
				// TODO Auto-generated method stub
				emptyview.setText("加载失败，稍后重试");
			}
		}, RankingTodayActivity.this);

	}

	@Override
	public void onCheckedChanged(CompoundButton button, boolean ischeck) {
		page = 1;
		if (ischeck) {
			switch (button.getId()) {
			case R.id.id_qy:
				state = "1";
				array = new JSONArray();
			//	adapter = new SelecMyColumnListAdapter(RankingTodayActivity.this, array);
				adapter = new RankingTodayAdapter(RankingTodayActivity.this, array);
				rankingListview.setAdapter(adapter);

				initData();
				break;

			case R.id.id_zj:
				state = "2";
				array = new JSONArray();
				//adapter = new SelecMyColumnListAdapter(RankingTodayActivity.this, array);
			adapter = new RankingTodayAdapter(RankingTodayActivity.this, array);
				rankingListview.setAdapter(adapter);

				initData();
				break;
			}

		}
	}

}
